package com.zoffy.wmsbackend.controller;

import com.zoffy.wmsbackend.common.BaseResponse;
import com.zoffy.wmsbackend.common.ErrorCode;
import com.zoffy.wmsbackend.common.ResultUtils;
import com.zoffy.wmsbackend.exception.BusinessException;
import com.zoffy.wmsbackend.model.dto.user.UserLoginDTO;
import com.zoffy.wmsbackend.model.dto.user.UserRegistryDTO;
import com.zoffy.wmsbackend.model.vo.UserVO;
import com.zoffy.wmsbackend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/registry")
    public BaseResponse<Boolean> registry(@RequestBody UserRegistryDTO userRegistryDTO){
        String userAccount = userRegistryDTO.getUserAccount();
        String userPassword = userRegistryDTO.getUserPassword();
        String checkPassword = userRegistryDTO.getCheckPassword();
        if(StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.registry(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }
    @PostMapping("/login")
    public BaseResponse<UserVO> login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request, HttpServletResponse response){
        String userAccount = userLoginDTO.getUserAccount();
        String userPassword = userLoginDTO.getUserPassword();
        if(StringUtils.isAnyBlank(userAccount,userPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO result = userService.login(userAccount, userPassword,request,response);
        return ResultUtils.success(result);
    }
    @GetMapping("/current")
    public BaseResponse<UserVO> current(){
        UserVO result = userService.current();
        return ResultUtils.success(result);
    }
}
